package com.base.video;

import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.sp.SpUtil;
import com.base.video.databinding.ActivityVideosettingBinding;
import com.base.video.exo.ExoPlayer;
import com.base.video.ijk.IjkPlayer;
import com.base.video.java.MediaPlayerManager;
import com.base.video.java.SystemMediaPlayer;

import static com.base.commonlib.sp.SpUtil.VIDEO_SETTING;

/**
 * Created by weikailiang on 2020/2/21.
 */
@Route(path = "/video/vodeosetting")
public class VideoSettingActivity extends CBaseBarMvpActivity{

    private ActivityVideosettingBinding mBinding;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_videosetting;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityVideosettingBinding) mRootBinding;
        setBackNavigation();
        setBarTitle("切换播放起");
    }

    @Override
    protected void start() {
        if (SpUtil.getString(VIDEO_SETTING)!=null) {
            if (SpUtil.getString(VIDEO_SETTING).equals("MediaPlayer")) {//MediaPlayer
                mBinding.checkmP.setChecked(true);
                MediaPlayerManager.instance().releasePlayerAndView(this);
                MediaPlayerManager.instance().setMediaPlayer(new SystemMediaPlayer());
            } else if (SpUtil.getString(VIDEO_SETTING).equals("IjkPlayer")) {//IjkPlayer
                MediaPlayerManager.instance().releasePlayerAndView(this);
                MediaPlayerManager.instance().setMediaPlayer(new IjkPlayer());
                mBinding.checkiP.setChecked(true);
            } else if (SpUtil.getString(VIDEO_SETTING).equals("ExoPlayer")) {//ExoPlayer
                MediaPlayerManager.instance().releasePlayerAndView(this);
                MediaPlayerManager.instance().setMediaPlayer(new ExoPlayer(this));
                mBinding.checkeP.setChecked(true);
            } else {
                MediaPlayerManager.instance().releasePlayerAndView(this);
                MediaPlayerManager.instance().setMediaPlayer(new SystemMediaPlayer());
                SpUtil.putString(VIDEO_SETTING,"MediaPlayer");
                mBinding.checkmP.setChecked(true);
            }
        }else {
            MediaPlayerManager.instance().releasePlayerAndView(this);
            MediaPlayerManager.instance().setMediaPlayer(new SystemMediaPlayer());
            SpUtil.putString(VIDEO_SETTING,"MediaPlayer");
            mBinding.checkmP.setChecked(true);
        }

        mBinding.rbVideo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.checkm_p){
                    SpUtil.putString(VIDEO_SETTING,"MediaPlayer");
                }else if (i==R.id.checki_p){
                    SpUtil.putString(VIDEO_SETTING,"IjkPlayer");
                }else if (i==R.id.checke_p){
                    SpUtil.putString(VIDEO_SETTING,"ExoPlayer");
                }
            }
        });
    }
}
